<?php


namespace app\admin\order\controller;


use app\admin\order\logic\OrderRefundLogic;
use app\admin\order\validate\OrderRefundValidate;
use app\common\controller\BaseAdminController;
use app\model\order\OrderRefund;
use support\utils\LockUtil;

/**
 * 售后订单
 * Class OrderController
 * @package app\admin\order\controller
 */
class RefundController extends BaseAdminController
{
    protected function init()
    {
        parent::init();
        $this->logic = new OrderRefundLogic();
        $this->validate = new OrderRefundValidate();
    }

    protected function searchWhere()
    {
        $where = [];
        $this->form['order_no'] && $where[] = ['order_no', 'like', "%{$this->form['order_no']}%"];
        $this->form['refund_order_no'] && $where[] = ['refund_order_no', 'like', "%{$this->form['refund_order_no']}%"];

        $this->form['start_time'] && $this->form['end_time'] && $where[] = ['create_time', 'between', [$this->form['start_time'], $this->form['end_time']]];
        return $where;
    }

    public function list()
    {
        $where = [];
        $this->form['status'] && $where[] = ['status', '=', $this->form['status']];

        $query = $this->logic->search(array_merge($this->searchWhere(), $where));
        $data = $this->logic->refundPageList($query);
        $badge = OrderRefund::where(array_merge($this->searchWhere()))
            ->field([
                'status',
                'count(1) as count'
            ])
            ->group('status')
            ->select();
        $data['status_badge'] = array_column(collect($badge)->toArray(), 'count', 'status');
        $data['status_badge'][''] = array_sum(array_values($data['status_badge']));

        return $this->success($data);
    }

    /**
     * 同意或者拒绝退款
     */
    public function read()
    {
        $this->validate->checkData(input(), 'id');
        $query = $this->logic->search(['id' => input('id')]);
        $query->with([
            'user' => function ($query) {
                $query->field([
                    "id",
                    "username",
                    "avatar",
                    "mobile",
                ]);
            }
        ]);
        $data = $query->find();
        return $this->success($data);
    }

    public function getStatusMap()
    {
        return $this->success([
            "status" => wrapperStatus(OrderRefund::STATUS_TEXT_MAP),
        ]);
    }

    /**
     * 同意
     */
    public function agree()
    {
        $input = input();
        $this->validate->checkData($input, 'agree');
        LockUtil::tryLock("refund:" . $input['id'], function () {
            $this->logic->transaction(function () {
                $this->logic->checkRefundMoney(input(), 'agree');
            });
        });

        return $this->success();
    }

    /**
     * 拒绝
     */
    public function refuse()
    {
        $input = input();
        $this->validate->checkData($input, 'refuse');
        LockUtil::tryLock("refund:" . $input['id'], function () {
            $this->logic->transaction(function () {
                $this->logic->checkRefundMoney(input(), 'refuse');
            });
        });

        return $this->success();
    }

}